<template>
  <div class="app-container">
    <el-card>
      <el-form :model="verifyOrgList" :inline="true" label-width="68px">
        <el-form-item label="商品名称">
          <el-select v-model="verifyOrgList.skuID" placeholder="请选择物料信息" clearable :style="{ width: '100%' }" remote
            :remote-method="searchSelectNccMaterial" filterable :loading="loading">
            <el-option v-for="item in materialNameOptions" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="批号">
          <el-input v-model="verifyOrgList.batchNo" placeholder="请输入商品批号" clearable style="width: 150px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-hasPermi="['organize:verifyOrg:query']"
            @click="getVerifyList">查询</el-button>
          <el-button type="danger" icon="el-icon-refresh" @click="clearVerify">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="fenyeList" height="605" style="width: 100%;" :default-sort = "{prop: 'date', order: 'descending'}">
        <el-table-column prop="code" label="商品编码" align="center"></el-table-column>
        <el-table-column prop="vbillcode" label="单据号" align="center"></el-table-column>
        <el-table-column prop="vbatchcode" label="批号" sortable align="center"></el-table-column>
        <el-table-column prop="vvendbatchcode" label="供应商批号" align="center"></el-table-column>
        <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="materialspec" label="商品规格" align="center"></el-table-column>
        <el-table-column prop="cwarehouse_name" label="仓库名称" align="center"></el-table-column>
        <el-table-column prop="sto_unit_id" label="货位号" align="center"></el-table-column>
        <el-table-column prop="pd_date" label="生产日期" align="center">
          <template #default="{ row }">
            {{ row.pd_date && row.pd_date !== "1900-01-01 00:00:00" ? formatDate(row.pd_date) : "" }}
          </template>
        </el-table-column>
        <el-table-column prop="exp_date" label="有效期" align="center">
          <template #default="{ row }">
            <!-- 仅当复验期无效或为空时，才显示有效期 -->
            {{ (!row.retest_date || row.retest_date === "1900-01-01 00:00:00")&&(row.exp_date && row.exp_date !== "1900-01-01 00:00:00") ? formatDate(row.exp_date) : "" }}
          </template>
        </el-table-column>
        <el-table-column prop="retest_date" label="复验期" align="center">
          <template #default="{ row }">
            <!-- 仅当复验期有效时才显示 -->
            {{ row.retest_date && row.retest_date !== "1900-01-01 00:00:00" ? formatDate(row.retest_date) : "" }}
          </template>
        </el-table-column>
        <el-table-column prop="StateValue" label="库存状态" align="center"></el-table-column>
        <el-table-column prop="sku_num" label="实际数量" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-hasPermi="['organize:verifyOrg:edit']" size="mini" type="text" icon="el-icon-edit"
              @click="editState(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background layout=" total,prev, pager, next, sizes,jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]" :total="total">
      </el-pagination>
    </el-card>
    <frozenOperation ref="frozenOperationRef" :visible.sync="visible" @successfun="getVerifyList"></frozenOperation>
  </div>
</template>

<script>
  import {
    getSelectNccMaterialInfo //物料模糊查询
  } from '@/api/dropDown.js'
  import {
    getVerityWarehouseStatistic, //获取统计数量
    getBillStatusAll //获取商品状态
  } from '@/api/report/index.js'
  import frozenOperation from '@/views/organize/goodsOrg/components/frozenOperation.vue'
  export default {
    data() {
      return {
        visible: false,
        total: 0,
        pageNum: 1,
        pageSize: 10,
        verifyOrgList: {
          // skuState: '1001A110000000002T08',
          // skuStateValue: 1,
          skuIDValue: 1,
          skuID: '',
          batchNo: '',
          batchNoValue: 1
        },
        dataList: [],
        stateList: [], //商品状态
        materialNameOptions: [], //物料查询
        loading: false
      }
    },
    components: {
      frozenOperation
    },
    created() {
      this.getVerifyList();
      this.getVerifyState(); //状态
    },
    computed: {
      fenyeList() {
        const startIndex = (this.pageNum - 1) * this.pageSize;
        return this.dataList.slice(startIndex, startIndex + this.pageSize);
      }
    },
    methods: {
      formatDate(date) {
        if (!date) return ''; // 如果 `date` 为空或 `null`，返回占位符
        return this.$dayjs(date).isValid() ? this.$dayjs(date).format('YYYY-MM-DD') : '-';
      },
      /**
       * 远程搜索函数
       * 获取NCC物料信息
       */
      // 远程搜索函数 获取NCC物料信息
      searchSelectNccMaterial(query) {
        if (query !== '') {
          this.loading = true; // 开始加载
          const params = {
            materialName: query, // 传入输入的关键字
          };
          // 调用接口进行物料信息查询
          getSelectNccMaterialInfo(params)
            .then(response => {
              if (response.code === 200) {
                // 更新物料选项
                this.materialNameOptions = response.data.map(item => ({
                  code: item.value, // 物料主键
                  name: item.label, // 物料名称
                }));
              } else {
                this.materialNameOptions = [];
              }
            })
            .catch(error => {
              console.error('查询物料信息失败:', error);
              this.materialNameOptions = [];
            })
            .finally(() => {
              this.loading = false; // 停止加载
            });
        } else {
          this.materialNameOptions = [];
        }
      },
      //修改状态
      editState(row) {
        this.visible = true;
        row.updateType = 1;
        this.$refs.frozenOperationRef.openDiolog(row);
      },
      //获取待验数据
      getVerifyList() {
        getVerityWarehouseStatistic(this.verifyOrgList).then(res => {
          // 检查接口返回数据的结构
          if (res.data) {
            this.dataList = res.data.type5 || [];
            this.total = res.data.type5.length;
          }
        }).catch(error => {
          this.$message.error('获取库存统计数据失败:', error);
        });
      },
      //重置数据
      clearVerify() {
        this.verifyOrgList.skuID = '';
        this.verifyOrgList.batchNo = '';
        this.getVerifyList();
      },
      //查询数据
      getVerifyState() {
        getBillStatusAll().then(res => {
          this.stateList = res.data;
        })
      },
      //分页
      handleSizeChange(newsize) {
        this.pageNum = 1
        this.pageSize = newsize
      },
      handleCurrentChange(newpage) {
        this.pageNum = newpage
      },

    }
  }
</script>

<style scoped lang="scss">
  .app-container {
    padding: 20px;
  }

  .el-card {
    margin-bottom: 20px;
  }

  .el-pagination {
    float: right;
    margin: 20px;
  }
</style>
